package com.lizardmind.haofudoctor.ui.diagnosis.visit

import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.adapter.BaseAdapter
import com.linxiao.framework.adapter.DividerGridItemDecoration
import com.linxiao.framework.util.color
import com.linxiao.framework.util.ke
import com.linxiao.framework.util.toasts
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ItemSendVisitBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.*
import com.lizardmind.haofudoctor.other.rongYun.HaofuMessage
import com.lizardmind.haofudoctor.other.rongYun.MessageTags
import com.lizardmind.haofudoctor.other.rongYun.USER_PREFIX
import io.reactivex.functions.Consumer
import io.rong.imkit.RongIM
import io.rong.imlib.RongIMClient
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.Message
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * 发送随访问诊单
 * Created by Extends on 2017/11/15 15:17
 */
class SendVisitActivity:BaseActivity() {

    private val apt by lazy { BaseAdapter<SendVisitBean,ItemSendVisitBinding>(R.layout.item_send_visit, arrayListOf()) }
    private val uid by lazy {intent.getStringExtra("uid")}
    private lateinit var rv: RecyclerView
    override fun onCreatRootView(): View? {
        return linearLayout {
            orientation = LinearLayout.VERTICAL
            include<ConstraintLayout>(R.layout.include_title_text)
            rv = recyclerView {
                id = R.id.test_recycler
                layoutManager = LinearLayoutManager(context)
                adapter =apt
                addItemDecoration(DividerGridItemDecoration(context, 2f, context.color(R.color.windowBackground)))
            }.lparams(-1,-1)
        }

    }

    override fun onInitView(savedInstanceState: Bundle?) {
        //初始化title
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "发送随访问诊单"
        apt.setEmptyView(LayoutInflater.from(this).inflate(R.layout.include_empty,this.rv,false))

        apt.onBind { itemBingding, position, data ->
            itemBingding.isvSend.onClick {
                //创建随访复诊单
                observe {
                    postApi<Request<String>>(hashMapOf(
                            "action" to "send_visit",
                            "token" to DoctorInfo.token,
                            "prescribingId" to data.id
                    ), Consumer {
                        it.dispose {
                            RongIM.getInstance().sendMessage(
                                    Message.obtain(USER_PREFIX+uid, Conversation.ConversationType.PRIVATE, HaofuMessage(jsonOf("id" to it,"type" to "2"))),
                                    MessageTags.HAOFU,
                                    null,
                                    object : io.rong.imlib.IRongCallback.ISendMessageCallback {
                                        override fun onAttached(p0: Message) {}
                                        override fun onSuccess(p0: Message) {
                                            toasts("发送成功")
                                            finish()
                                        }
                                        override fun onError(p0: Message?, p1: RongIMClient.ErrorCode) { toasts("发送失败") }
                                    }
                            )
                        }
                    })
                }
            }
        }
        

        observe {
            postApi<Request<List<SendVisitBean>>>(hashMapOf(
                    "action" to "get_visit_list",
                    "token" to DoctorInfo.token,
                    "uid" to uid
            ), Consumer {
                it.dispose {
                    apt.clearAddAllData(it)
                }
            },context = this)
        }
    }
}